package com.meiqiongproperty.yiguanjia.fragment.home.tenement;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.ParkingfeeLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.pay.PayTypeFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendCarFee;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendgetCar;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.CarBean;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.ChargeCarZFB;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayWX;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.ui.dialog.PersonalCenterAlertDialog;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.FlowLayout;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.TagAdapter;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.TagFlowLayout;
import com.meiqiongproperty.yiguanjia.ui.picker.DataPickerDialog;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.tenement
 * @User ChenJing
 * @Date 2017/6/7
 */

public class ParkingFeeFragment extends BaseFragment{
    ParkingfeeLayoutBinding binding;
    private String[] times = new String[]{"3个月", "6个月", "12个月"};
    int type,mouth,carid,choesspostion;
    CarBean bean;
    List<CarBean.CarBeans> list;
    Dialog chooseDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.parkingfee_layout, container, false);
        binding.setClick(this);
        getCar();
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.navigation.setTitle("停车费");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.address.setText(DBUserUtils.getAddress(getActivity()));
        binding.publicTimeFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                switch (position){
                    case 0:
                        mouth=3;
                        break;
                    case 1:
                        mouth=6;
                        break;
                    case 2:
                        mouth=12;
                        break;
                }
                binding.amount.setText(Utils.toString(Utils.toDouble(list.get(choesspostion).getPrice())*mouth));
                return true;
            }
        });
        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
        binding.publicTimeFlow.setAdapter(new TagAdapter<String>(times) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv=(TextView)mInflater.inflate(R.layout.public_time_item,binding.publicTimeFlow,false);
                tv.setText(s);
                return tv;
            }
        });


    }

    public void commitPay(View view) {
        SendChargePropertyMethod();
    }

    private void SendChargePropertyMethod() {
        SendCarFee sendChargeProperty=new SendCarFee();
        sendChargeProperty.setPayMethod(type);
        sendChargeProperty.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        sendChargeProperty.setRoomID(DBUserUtils.getmDefaultHouse(getActivity()).getID());
        sendChargeProperty.setMonth(mouth);
        sendChargeProperty.setCarId(carid);
        switch (type){
            case 1:
                ApiService.chargeCar(getActivity(), new SimpleHttpCallback<ChargeCarZFB>() {
                    @Override
                    public void onNext(ChargeCarZFB commPayZFB) {
                        String orderInfo = commPayZFB.getParameter().getParameter();   // 订单信息
                        doAlipay(orderInfo);
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
            case 2:
                ApiService.chargePropertyWX(getActivity(), new SimpleHttpCallback<CommPayWX>() {
                    @Override
                    public void onNext(CommPayWX commPayWX) {
                        doWXPay(commPayWX.getParameter().getParameter(),commPayWX.getParameter().getParameter().getAppid());
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
        }

    }

    public void select_pay_type(View view){
        PayTypeFragment payTypeSeleor = new PayTypeFragment();
        payTypeSeleor.setPayTypeClickListener(new PayTypeFragment.PayTypeClickListener() {
            @Override
            public void setData(Object data) {
                type= (int) data;
                switch (type){
                    case 1:
                        binding.type.setText("支付宝支付");
                        break;
                    case 2:
                        binding.type.setText("微信支付");
                        break;
                }
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), payTypeSeleor, payTypeSeleor.getTagName());
    }

    public void getCar() {
        SendgetCar send=new SendgetCar();
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setToken(Utils.getToken(getActivity()));
        ApiService.getCar(getActivity(), new IHttpCallback<CarBean>() {
            @Override
            public void onNext(CarBean carBean) {
                binding.setBean(carBean.getCar().get(0));
                bean=carBean;
                list=carBean.getCar();
            }

            @Override
            public void onError(int code, String message) {
                PersonalCenterAlertDialog dialog = new PersonalCenterAlertDialog(getActivity());
                dialog.builder().setCanceledOnTouchOutside(false).setMsg("暂时没有已登记的车辆")
                        .setPositiveButton("返回上一页", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                sendKeyBackEvent();
                            }
                        }).show();
                LogUtil.d("停车费==============>"+message);
            }
        },Utils.getRequestBody(send));
    }

    public void chooesCarNum(View v){
        carid=list.get(0).getId();
        List<String> listString=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            listString.add(list.get(i).getCarNumber());
        }
        showChooseDialog(listString);
        LogUtil.d("list.get(choesspostion).getPrice()="+list.get(choesspostion).getPrice()+"mouth="+mouth);
        binding.amount.setText(Utils.toString(Utils.toDouble(list.get(choesspostion).getPrice())*mouth));
    }

    private void showChooseDialog(List<String> mlist) {
        final DataPickerDialog.Builder builder = new DataPickerDialog.Builder(getActivity());
        chooseDialog = builder.setData(mlist).setSelection(1).setTitle("取消")
                .setOnDataSelectedListener(new DataPickerDialog.OnDataSelectedListener() {
                    @Override
                    public void onDataSelected(String itemValue, int position) {
                        carid=list.get(position).getId();
                        choesspostion=position;
                        binding.carnum.setText(itemValue);
                    }

                    @Override
                    public void onCancel() {

                    }
                }).create();

        chooseDialog.show();
    }
}
